<script setup>
import { ref } from 'vue';
import { ElMessage } from 'element-plus'
// 定义状态变量来控制登录和注册表单的显示
const isLoginActive = ref(false);

// 方法：切换到登录表单
const switchToLogin = () => {
    if (!isLoginActive.value) {
        isLoginActive.value = true;
    }
};

// 方法：切换到注册表单
const switchToRegister = () => {
    if (isLoginActive.value) {
        isLoginActive.value = false;
    }
};
//控制注册与登录表单的显示， 默认显示注册
// const isRegister = ref(false)
//定义绑定参数数据
const registerDate = ref({
    username: '',
    password: '',
    rePassword: ''
})
// 将创建好的路由导入使用来进行页面跳转
import { useRouter } from 'vue-router';
const router = useRouter();
// 登录事件
const login = async () => {
    if (registerDate.value.username == '' && registerDate.value.password == '') {
        ElMessage.error('请输入账号密码！')
        return;
    }
    if (registerDate.value.username != 'admin') {
        ElMessage.error('账号错误！！')
        clear()
        return;
    }
    if (registerDate.value.password != '123456') {
        ElMessage.error('密码错误！！')
        clear()
        return;
    }
    ElMessage.success('登录成功!')
    // 在登录成功之后跳转到主页面
    router.push('/')
}
// 定义注册事件
const register = async () => {
    if (registerDate.value.password != registerDate.value.rePassword) {
        ElMessage.error('两次密码不一致！')
        registerDate.value.rePassword = '';
        return;
    }
    ElMessage.success('注册成功!')
    isLoginActive.value = true;
    // 在注册成功之后跳转到登录页面
    router.push('/login');
    clear();
}

// 将输入数据进行清空
const clear = () => {
    registerDate.value = {
        username: '',
        password: '',
        rePassword: ''
    }
}
</script>
<template>
    <div class="container">
        <div class="register-box" :class="{ 'slide-up': isLoginActive }">
            <h2 class="register-title" @click="switchToRegister">
                <span>没有账号，去</span>注册
            </h2>
            <div class="input-box">
                <input type="text" v-model="registerDate.username" placeholder="用户名">
                <input type="password" v-model="registerDate.password" placeholder="密码">
                <input type="password" v-model="registerDate.rePassword" placeholder="确认密码">
            </div>
            <button @click="register">注册</button>
        </div>
        <div class="login-box" :class="{ 'slide-up': !isLoginActive }">
            <div class="center">
                <h2 class="login-title" @click="switchToLogin">
                    <span>已有账号，去</span>登录
                </h2>
                <div class="input-box">
                    <input type="text" v-model="registerDate.username" placeholder="用户名">
                    <input type="password" v-model="registerDate.password" placeholder="密码">
                </div>
                <button @click="login">登录</button>
            </div>
        </div>
    </div>
    <div class="square">
        <ul>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
        </ul>
    </div>
    <div class="circle">
        <ul>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
        </ul>
    </div>
</template>

<style lang="scss" scoped>
* {
    margin: 0;
    padding: 0;
}

body {
    min-height: 100vh;
    /* 弹性布局 居中 */
    display: flex;
    justify-content: center;
    align-items: center;
    background-color: #177a0e60;
}

.container {
    background-color: #222;
    width: 350px;
    height: 550px;
    border-radius: 15px;
    overflow: hidden;
    position: relative;
    left: 40%;
    bottom: 40%;
}

.container::after {
    content: "";
    position: absolute;
    top: 0;
    left: 0;
    bottom: 0;
    right: 0;
    background: url("@/assets/bg.jpg") no-repeat;
    background-size: 500px;
    background-position: left bottom;
    opacity: 0.8;
}

/* 注册区域（登录区域很多样式和注册区域的一样，故而一些统一的样式写在了一起） */
.register-box {
    width: 70%;
    position: absolute;
    z-index: 1;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    transition: 0.3s ease;
}

.register-title,
.login-title {
    color: #fff;
    font-size: 27px;
    text-align: center;
}

.register-title span,
.login-title span {
    color: rgba(0, 0, 0, 0.4);
    display: none;
}

.register-box .input-box,
.login-box .input-box {
    background-color: #fff;
    border-radius: 15px;
    overflow: hidden;
    margin-top: 50px;
    opacity: 1;
    visibility: visible;
    transition: 0.6s ease;
}

.register-box input,
.login-box input {
    width: 100%;
    height: 30px;
    border: none;
    border-bottom: 1px solid rgba(0, 0, 0, 0.1);
    font-size: 12px;
    padding: 8px 0;
    text-indent: 15px;
    outline: none;
}

.register-box input:last-child,
.login-box input:last-child {
    border-bottom: none;
}

.register-box input::placeholder,
.login-box input::placeholder {
    color: rgba(0, 0, 0, 0.4);
}

.register-box button,
.login-box button {
    width: 100%;
    padding: 15px 45px;
    margin: 15px 0;
    background: rgba(0, 0, 0, 0.4);
    border: none;
    border-radius: 15px;
    color: rgba(255, 255, 255, 0.8);
    font-size: 13px;
    font-weight: bold;
    cursor: pointer;
    opacity: 1;
    visibility: visible;
    transition: 0.3s ease;
}

.register-box button:hover,
.login-box button:hover {
    background-color: rgba(0, 0, 0, 0.8);
}

/* 登录区域 */
.login-box {
    position: absolute;
    inset: 0;
    top: 20%;
    z-index: 2;
    background-color: #fff;
    transition: 0.3s ease;
}

.login-box::before {
    content: "";
    background-color: #fff;
    width: 200%;
    height: 250px;
    border-radius: 50%;
    position: absolute;
    top: -20px;
    left: 50%;
    transform: translateX(-50%);
}

.login-box .center {
    width: 70%;
    position: absolute;
    z-index: 3;
    left: 50%;
    top: 40%;
    transform: translate(-50%, -50%);
}

.login-title {
    color: #000;
}

.login-box .input-box {
    border: 1px solid rgba(0, 0, 0, 0.1);
}

.login-box button {
    background-color: #75a297;
}

/* 注册、登录区域收起 */
.login-box.slide-up {
    top: 90%;
}

.login-box.slide-up .center {
    top: 10%;
    transform: translate(-50%, 0%);
}

.login-box.slide-up .login-title,
.register-box.slide-up .register-title {
    font-size: 16px;
    cursor: pointer;
}

.login-box.slide-up .login-title span,
.register-box.slide-up .register-title span {
    margin-right: 5px;
    display: inline-block;
}

.login-box.slide-up .input-box,
.login-box.slide-up .button,
.register-box.slide-up .input-box,
.register-box.slide-up .button {
    opacity: 0;
    visibility: hidden;
}

.register-box.slide-up {
    top: 6%;
    transform: translate(-50%, 0%);
}

ul li {
    position: absolute;
    border: 1px solid #3630e8;
    background-image: linear-gradient(to bottom right, blue, rgb(226, 91, 163));
    width: 30px;
    height: 30px;
    list-style: none;
    opacity: 0;
}

.square li {
    top: 40vh;
    left: 60vw;
    /* 执行动画：动画名 时长 线性的 无限次播放 */
    animation: square 10s linear infinite;
}

.square li:nth-child(2) {
    top: 80vh;
    left: 10vw;
    /* 设置动画延迟时间 */
    animation-delay: 2s;
}

.square li:nth-child(3) {
    top: 80vh;
    left: 85vw;
    /* 设置动画延迟时间 */
    animation-delay: 4s;
}

.square li:nth-child(4) {
    top: 10vh;
    left: 70vw;
    /* 设置动画延迟时间 */
    animation-delay: 6s;
}

.square li:nth-child(5) {
    top: 10vh;
    left: 10vw;
    /* 设置动画延迟时间 */
    animation-delay: 8s;
}

.circle li {
    bottom: 0;
    left: 15vw;
    /* 执行动画 */
    animation: circle 10s linear infinite;
}

.circle li:nth-child(2) {
    left: 35vw;
    /* 设置动画延迟时间 */
    animation-delay: 2s;
}

.circle li:nth-child(3) {
    left: 55vw;
    /* 设置动画延迟时间 */
    animation-delay: 6s;
}

.circle li:nth-child(4) {
    left: 75vw;
    /* 设置动画延迟时间 */
    animation-delay: 4s;
}

.circle li:nth-child(5) {
    left: 90vw;
    /* 设置动画延迟时间 */
    animation-delay: 8s;
}

/* 定义动画 */
@keyframes square {
    0% {
        transform: scale(0) rotateY(0deg);
        opacity: 1;
    }

    100% {
        transform: scale(5) rotateY(1000deg);
        opacity: 0;
    }
}

@keyframes circle {
    0% {
        transform: scale(0) rotateY(0deg);
        opacity: 1;
        bottom: 0;
        border-radius: 0;
    }

    100% {
        transform: scale(5) rotateY(1000deg);
        opacity: 0;
        bottom: 90vh;
        border-radius: 50%;
    }
}
</style>